// import Vue from 'vue'
import VueRouter from 'vue-router'
import { createRouterGuards } from './routerGuards.js'
import Layout from '@/layout/index.vue'

// 解决重复点击路由报错的bug vue-router 3.2.0之后需要解开此注释--解决重复点击同一路由导致的错误
// const originalPush = VueRouter.prototype.push
// VueRouter.prototype.push = function push(location) {
//   return originalPush.call(this, location).catch(err => err)
// }

// 路由默认列表(所有角色都能访问的路由列表)
export const constantRoutes = [
  // 路由重定向--中转页面
  {
    path: '/redirect/:path(.*)',
    name: 'redirect',
    component: () => import(/* webpackChunkName: "redirect" */ '@/layout/redirect/index.vue'),
    hidden: true,
    meta: {
      title: '刷新中...',
      hidden: true
    }
  },
  {
    path: '/404',
    name: '404',
    component: () => import(/* webpackChunkName: "404" */ '@/layout/404/index.vue'),
    meta: {
      title: '404',
      hidden: true
    }
  },
  {
    path: '/login',
    name: 'login',
    component: () => import(/* webpackChunkName: "about" */ '@/views/login/index.vue'),
    meta: {
      title: '登录',
      hidden: true
    }
  },
  {
    path: '/',
    name: 'layout',
    redirect: '/dashboard',
    component: Layout,
    meta: {
      title: 'dashboard',
      icon: 'el-icon-eleme'
    },
    children: [
      {
        path: 'dashboard',
        name: 'dashboard',
        component: () => import(/* webpackChunkName: 'dashboard' */ '@/views/dashboard/index.vue'),
        meta: {
          title: 'dashboard',
          icon: 'el-icon-eleme',
          activeMenu: '/',
          alwaysShow: true
        }
      }
    ]
  },
  {
    path: '/example',
    name: 'example',
    redirect: '/example/table',
    component: Layout,
    meta: {
      title: 'example',
      icon: 'el-icon-eleme'
      // keepAlive: true
    },
    children: [
      {
        path: 'table',
        name: 'table',
        component: () => import(/* webpackChunkName: 'table' */ '@/views/table/index.vue'),
        meta: {
          title: 'table',
          icon: '',
          keepAlive: true
        }
      },
      {
        path: 'tree',
        name: 'tree',
        component: () => import(/* webpackChunkName: 'tree' */ '@/views/tree/index.vue'),
        meta: {
          title: 'tree',
          icon: '',
          keepAlive: true
        }
      }
    ]
  },
  {
    path: '/nested',
    component: Layout,
    redirect: '/nested/menu1',
    name: 'nested',
    meta: {
      title: 'Nested',
      icon: 'el-icon-eleme'
    },
    children: [
      {
        path: 'menu1',
        component: () => import('@/views/nested/menu1/index'), // Parent router-view
        name: 'menu1',
        redirect: '/nested/menu1/menu1-1',
        meta: { title: 'Menu1' },
        children: [
          {
            path: 'menu1-1',
            component: () => import('@/views/nested/menu1/menu1-1'),
            name: 'menu1-1',
            meta: { title: 'Menu1-1' }
          },
          {
            path: 'menu1-2',
            component: () => import('@/views/nested/menu1/menu1-2'),
            name: 'menu1-2',
            redirect: '/nested/menu1/menu1-2/menu1-2-1',
            meta: { title: 'Menu1-2' },
            children: [
              {
                path: 'menu1-2-1',
                component: () => import('@/views/nested/menu1/menu1-2/menu1-2-1'),
                name: 'menu1-2-1',
                meta: { title: 'Menu1-2-1' }
              },
              {
                path: 'menu1-2-2',
                component: () => import('@/views/nested/menu1/menu1-2/menu1-2-2'),
                name: 'menu1-2-2',
                meta: { title: 'Menu1-2-2' }
              }
            ]
          },
          {
            path: 'menu1-3',
            component: () => import('@/views/nested/menu1/menu1-3'),
            name: 'menu1-3',
            meta: { title: 'Menu1-3' }
          }
        ]
      },
      {
        path: 'http://www.baidu.com',
        component: () => import('@/views/nested/menu2/index'),
        name: 'menu2',
        meta: { title: 'menu2' }
      }
    ]
  },
  {
    path: '/echarts',
    component: Layout,
    redirect: '/echarts/echartsList',
    name: 'echarts',
    meta: {
      title: 'echarts',
      icon: 'el-icon-eleme'
    },
    children: [
      {
        path: 'echartsList',
        component: () => import('@/views/echarts/index.vue'),
        name: 'echartsList',
        meta: { title: 'echartsList' }
      }
    ]
  },
  {
    path: '/tinymce',
    component: Layout,
    redirect: '/tinymce/rich-edit',
    name: 'tinymce',
    meta: {
      title: 'tinymce',
      icon: 'el-icon-eleme'
    },
    children: [
      {
        path: 'rich-edit',
        component: () => import('@/views/tinymceEdit/index.vue'),
        name: 'richEdit',
        meta: {
          title: 'richEdit'
        }
      }
    ]
  }
]

// 异步路由列表(根据角色配置是否能够访问)
export const asyncRoutes = [
  {
    path: '/permission',
    name: 'permission',
    redirect: '/permission/page',
    component: Layout,
    meta: {
      title: 'permission',
      icon: 'el-icon-eleme',
      roles: [1, 2] // 1:superAdmin, 2: Admin, 3: user
    },
    children: [
      {
        path: 'page',
        name: 'page',
        component: () => import(/* webpackChunck 'page' */ '@/views/permission/page/index.vue'),
        meta: {
          title: 'page',
          roles: [1, 2] // 1:superAdmin, 2: Admin
        }
      }
    ]
  },
  // 匹配所有的*必须要放到最后才能生效
  {
    path: '*',
    redirect: {
      name: '404'
    },
    meta: {
      title: '404',
      hidden: true
    }
  }
]

// router实例
const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes: constantRoutes,
  scrollBehavior: () => {
    return { x: 0, y: 0 }
  }
})

export function setupRouter(Vue) {
  Vue.use(VueRouter)
  // 创建路由守卫
  createRouterGuards(router)
}

export default router
